package com.zhq;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Generator {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai";
        String username = "root";
        String password = "123456";
        FastAutoGenerator.create(url, username, password)
                .globalConfig((scanner, builder) -> builder.author(scanner.apply("请输入作者名称？"))
                        .outputDir(scanner.apply("请输入输出路径？"))
                )
                .packageConfig((scanner, builder) -> builder.parent(scanner.apply("请输入包名？")))
                .strategyConfig((scanner, builder) -> builder
                        .addInclude(getTables(scanner.apply("请输入表名，多个英文逗号分隔？所有输入ALL")))
                        .addTablePrefix("tb_")     // 去掉表前缀
                        .entityBuilder()
                        .enableLombok()
                        
                        .addTableFills(
                                new Column("create_time", FieldFill.INSERT)
                        )
                        .build())
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }
    
    protected static List<String> getTables(String tables) {
        return "ALL".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
